package com.dianshang.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dianshang.dao.AdDao;
import com.dianshang.pojo.business.Ad;
import com.dianshang.service.business.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class AdServiceImpl implements AdService {

    @Autowired
    private AdDao adDao;

    @Autowired
    private RedisTemplate redisTemplate; // SpringDataRedis

    /**
     * 查询首页位置,轮播图广告的数据(Thymeleaf实现)
     * 将轮播图加载到缓存中
     */
    @Override
    public List<Ad> findIndexAll() {
        Example example = new Example(Ad.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("position","web_index_lb"); // 广告位置
        criteria.andEqualTo("status","1"); // 状态
        criteria.andLessThanOrEqualTo("startTime", new Date()); // andLessThanOrEqualTo <= 当前时间
        criteria.andGreaterThanOrEqualTo("endTime", new Date()); // andGreaterThanOrEqualTo >= 当前时间
        List<Ad> ads = adDao.selectByExample(example);
        // 广告数据加载到缓存——>String类型
        redisTemplate.boundValueOps("Ad_String").set(ads); // String类型
        redisTemplate.boundHashOps("Ad_Map").put("广告数据",ads); // Hash类型
        return ads;
    }

}